package com.dsbj.mp.icardservice.utils.jwt;

import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;


public class RsaKeyHelper {
	
	/**
	 * 获取公钥
	 *
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public PublicKey getPublicKey(String filename) throws Exception {
		InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
		DataInputStream dis = new DataInputStream(resourceAsStream);
		byte[] keyBytes = new byte[resourceAsStream.available()];
		dis.readFully(keyBytes);
		dis.close();
		X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePublic(spec);
	}
	
	/**
	 * 获取密钥
	 *
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public PrivateKey getPrivateKey(String filename) throws Exception {
		InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
		DataInputStream dis = new DataInputStream(resourceAsStream);
		byte[] keyBytes = new byte[resourceAsStream.available()];
		dis.readFully(keyBytes);
		dis.close();
		PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePrivate(spec);
	}
	
	/**
	 * 生存rsa公钥和密钥
	 *
	 * @param publicKeyFilename
	 * @param privateKeyFilename
	 * @param password
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 */
	public static void generateKey(String publicKeyFilename, String privateKeyFilename, String password) throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(1024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
		FileOutputStream fos = new FileOutputStream(publicKeyFilename);
		fos.write(publicKeyBytes);
		fos.close();
		byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
		fos = new FileOutputStream(privateKeyFilename);
		fos.write(privateKeyBytes);
		fos.close();
	}
	
	/**
	 * 生存rsa公钥和密钥
	 *
	 * @param password
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 */
	public static Map<String, byte[]> generateKey(String password) throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(1024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
		byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
		Map<String, byte[]> map = new HashMap<String, byte[]>();
		map.put("pub", publicKeyBytes);
		map.put("pri", privateKeyBytes);
		return map;
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2019-11-26
	 */
	public static void main(String[] args){
		/*
		String baseFilepath="./dsbj-mp-icard-service/src/main/resources/";
		String pubKeyFilepath=baseFilepath+"/jwt/pub.key";
		String priKeyFilepath=baseFilepath+"/jwt/pri.key";
		
		try {
			generateKey(pubKeyFilepath,priKeyFilepath,"dsbj-mp");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		System.out.println("生成公钥私钥,已完成..");
		*/
		
		
		
		
	}
	
}

